﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AI.Steering
{
    /// <summary>
    /// 徘徊
    /// </summary>
    public class SteeringForWander : Steering
    {
        [Tooltip("徘徊圆半径")]
        public float wandeRadius = 5;
        [Tooltip("与徘徊圆的间距")]
        public float wandeDistance = 10;
        public Vector3 targetPoint;
        private IEnumerator iterator;
        private void OnEnable()
        {
            iterator = ChangeTargetPoint();
            StartCoroutine(iterator);
        }
        private void OnDisable()
        {
            StopCoroutine(iterator);
        }
        public override Vector3 GetForce()
        {
            //计算实际力
            //期望力 = 目标点 - 自身点
            Vector3 expectForce = (targetPoint - transform.position).normalized * speed;
            //实际力 = 期望力 - 当前力
            return (expectForce - controller.currentForce) * weight;//权重越大，实际力到打最大越快
        }
        public float changeInterval = 3;
        private IEnumerator ChangeTargetPoint()
        {
            while (true)
            {
                Vector3 randomPoint;
                //生成随机点
                if (controller.isPlane)
                {
                    //--圆上
                    randomPoint = UnityEngine.Random.insideUnitCircle.normalized * wandeRadius;
                    randomPoint = new Vector3(randomPoint.x, 0, randomPoint.y);
                }
                else
                {
                    //--球上
                    randomPoint = UnityEngine.Random.onUnitSphere * wandeRadius;
                }
                //设置徘徊圆位置
                targetPoint = transform.position + transform.forward * wandeDistance + randomPoint;
                yield return new WaitForSeconds(changeInterval);//等待
            }
        }
        private void Update()
        {
            Debug.DrawLine(transform.position, targetPoint);
        }
    }
}

